package com.timmy.chi.ui.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.timmy.chi.R;
import com.timmy.chi.adapter.HouTaiAdapter;
import com.timmy.chi.model.beans.HouTai;
import com.timmy.chi.utils.network.MyHttp;
import com.timmy.chi.utils.network.ResultCallback;

import java.util.Collections;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 后台
 */

public class HouTaiActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private ListView mListView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private Gson mGson;
    private HouTaiAdapter mOrderAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_houtai);
        ButterKnife.bind(this);
        initView();

        mOrderAdapter = new HouTaiAdapter(this);

        mListView.setAdapter(mOrderAdapter);
        mListView.setOnItemClickListener(this);

        mGson = new Gson();

        initData();
    }

    private void initData() {
        MyHttp.getHouTaiData(new ResultCallback() {
            @Override
            public void onSuccess(String s) {

                mSwipeRefreshLayout.setRefreshing(false);

                HouTai houTai = mGson.fromJson(s, HouTai.class);
                if (houTai.getResponseState() == 0) {
                    List<HouTai.OrdersBean> orders = houTai.getOrders();
                    // 添加商品
                    addData(orders);
                }
            }

            @Override
            public void onError() {
                mSwipeRefreshLayout.setRefreshing(false);
                Toast.makeText(HouTaiActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void addData(List<HouTai.OrdersBean> orders) {
        for (HouTai.OrdersBean order : orders) {

            List<HouTai.OrdersBean.FoodStatesBean> foodStates = order.getFoodStates();

            //for (HouTai.OrdersBean.FoodStatesBean foodState : foodStates) {
            Collections.reverse(foodStates);

            mOrderAdapter.setList(foodStates);
            // }
        }
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.listview);
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_order);

        mSwipeRefreshLayout.setOnRefreshListener(mOnRefreshListener);
        mSwipeRefreshLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.YELLOW);
    }

    private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mOrderAdapter.cleaData();
            initData();
        }
    };

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HouTai.OrdersBean.FoodStatesBean item = mOrderAdapter.getItem(position);
        final int sid = item.getSid();

        if (sid == 2) {
            Toast.makeText(HouTaiActivity.this, "该菜品已烹饪", Toast.LENGTH_SHORT).show();
            return;
        }
        if (sid == 3) {
            Toast.makeText(HouTaiActivity.this, "该菜品已撤销", Toast.LENGTH_SHORT).show();
            return;
        }

        final int foodId = item.getId();

        Dialog dialog = new AlertDialog.Builder(this).setTitle(null).setMessage("确定烹饪吗？")
                .setNegativeButton("再想想", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        MyHttp.updateState(foodId, new ResultCallback() {
                            @Override
                            public void onSuccess(String s) {

                                Log.i("sss", "onSuccess: " + s);
                                Toast.makeText(HouTaiActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                initData();
                            }

                            @Override
                            public void onError() {
                                Toast.makeText(HouTaiActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                }).show();
        dialog.setCanceledOnTouchOutside(false);


    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        Intent intent = new Intent(this, LogInActivity.class);
        startActivity(intent);
        finish();

    }
}
